package com.twodstar.selfie.slidemenu;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.view.MenuItem;
import com.chartboost.sdk.CBPreferences;
import com.chartboost.sdk.Chartboost;
import com.chartboost.sdk.Chartboost.CBAgeGateConfirmation;
import com.chartboost.sdk.ChartboostDelegate;
import com.chartboost.sdk.Model.CBError.CBClickError;
import com.chartboost.sdk.Model.CBError.CBImpressionError;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;
import com.twodstar.selfie.R;



public class BaseActivity extends SlidingFragmentActivity implements ChartboostDelegate
{

	protected Fragment mContent;
	private int mTitleRes;
	protected SlideMenuFragment mFrag;
	private Chartboost cb;
	private InterstitialAd interstitialAd;
		
	
	@Override
	public void onSaveInstanceState(Bundle outState) 
	{
		super.onSaveInstanceState(outState);
		getSupportFragmentManager().putFragment(outState, "mContent", mContent);
	}

	public BaseActivity() 
	{
		mTitleRes = R.string.app_name;
	}
	
	public Fragment GetActiveFragment()
	{
		return mContent;
	}
	
	@Override
	protected void onDestroy() 
	{
		// TODO Auto-generated method stub
		super.onDestroy();
		this.cb.onDestroy(this);
	}
	
	@Override
	protected void onStop() 
	{
		// TODO Auto-generated method stub
		super.onStop();
		this.cb.onStop(this);
	}
	
	@Override
	public void onBackPressed() 
	{

	    // If an interstitial is on screen, close it. Otherwise continue as normal.
	    if (this.cb.onBackPressed())
	    {
	    	if(this.cb.hasCachedInterstitial()== false)
	        {
	    		cb.cacheInterstitial();
	        }
	        return;
	    }
	    else
	    {
	       super.onBackPressed();
	    }
	}
	
	@Override
    protected void onResume() 
	{
      
        super.onResume();
        if(this.cb.hasCachedInterstitial()== false)
        {
        	
        	cb.cacheInterstitial();
        }
        
        
    }

	@Override
	public void onCreate(Bundle savedInstanceState) 
	{
		
		super.onCreate(savedInstanceState);
		
		setTitle(mTitleRes);
		
		this.cb = Chartboost.sharedChartboost();
		String appId = "52d8bc96f8975c211e08c4b8";
		String appSignature = "92574079f000e0b272959464e1219a0453ea6114";
		this.cb.onCreate(this, appId, appSignature, null);
		
		InitAdmob();

		// set the Behind View
		setBehindContentView(R.layout.menu_frame);
		if (savedInstanceState == null) 
		{
			FragmentTransaction t = this.getSupportFragmentManager().beginTransaction();
			
			mFrag = new SlideMenuFragment();
			
			t.replace(R.id.menu_frame, mFrag);
			t.commit();
		} else {
			mFrag = (SlideMenuFragment)this.getSupportFragmentManager().findFragmentById(R.id.menu_frame);
		}

		// customize the SlidingMenu
		SlidingMenu sm = getSlidingMenu();
		sm.setShadowWidthRes(R.dimen.shadow_width);
		sm.setShadowDrawable(R.drawable.shadow);
		sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
		sm.setFadeDegree(0.35f);
		sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);

		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		
		// set the Above View
		if (savedInstanceState != null)
			mContent = getSupportFragmentManager().getFragment(savedInstanceState, "mContent");
		
		if (mContent == null)
			mContent = new SherlockFragment();	
		
		// set the Above View
		setContentView(R.layout.content_frame);
		
		getSupportFragmentManager()
		.beginTransaction()		
			.replace(R.id.content_frame, mContent)
		.commit();
		
		// set the Behind View
		setBehindContentView(R.layout.menu_frame);
		getSupportFragmentManager()
		.beginTransaction()
		.replace(R.id.menu_frame, new SlideMenuFragment())
		.commit();
		
		// customize the SlidingMenu
		getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
		
		
		getSupportActionBar().setStackedBackgroundDrawable(getResources().getDrawable( R.drawable.title_bar_background ));
		getSupportActionBar().setBackgroundDrawable(getResources().getDrawable( R.drawable.title_bar_background )); 
		getSupportActionBar().setDisplayShowTitleEnabled(false);		  
		getSupportActionBar().setIcon(R.drawable.mondobar_icon_launcher_normal);
		
		
		
				
	}
	
	private void InitAdmob()
	{
		interstitialAd = new InterstitialAd(this);
		interstitialAd.setAdUnitId("ca-app-pub-3356878628863953/1941117827");
		
		 // Create ad request.
	    AdRequest adRequest = new AdRequest.Builder().build();

	    // Begin loading your interstitial.
	    interstitialAd.loadAd(adRequest);
	    
	 // Set the AdListener.
	    interstitialAd.setAdListener(new AdListener() 
	    {
	      @Override
	      public void onAdLoaded() 
	      {
	         
	     
	      }

	      @Override
	      public void onAdFailedToLoad(int errorCode) 
	      {
	    	  // Create ad request.
	  	    AdRequest adRequest = new AdRequest.Builder().build();

	  	    // Begin loading your interstitial.
	  	    interstitialAd.loadAd(adRequest);
	      }
	      
	      @Override
	      public void onAdOpened() 
	      {
	    	  
	      }
	      
	      @Override
	      public void onAdClosed() 
	      {
	    	  // Create ad request.
	  	    AdRequest adRequest = new AdRequest.Builder().build();

	  	    // Begin loading your interstitial.
	  	    interstitialAd.loadAd(adRequest);
	      }
	      
	      
	      @Override
	      public void onAdLeftApplication() 
	      {
	    	  // Create ad request.
	  	    AdRequest adRequest = new AdRequest.Builder().build();

	  	    // Begin loading your interstitial.
	  	    interstitialAd.loadAd(adRequest);
	      }

	      
	     
	      
	    });
	}
	
	 @Override
	 protected void onStart() 
	 {
		super.onStart();
	
	   this.cb.onStart(this);
	
	    // Notify the beginning of a user session. Must not be dependent on user actions or any prior network requests.
	   this.cb.startSession();
	    
	   CBPreferences.getInstance().setImpressionsUseActivities(true);
	
	    // Show an interstitial
	   this.cb.cacheInterstitial();
		
	 }
	 
	protected void AdRequestFullScreen() 
	{
		
		if(this.cb.hasCachedInterstitial())
		{
			this.cb.showInterstitial();
			Log.i("game", "Showing Full Screen Ad");
			//SDLActivity.handlePause();
		}
		else
		{
			if (interstitialAd.isLoaded()) 
			{
				interstitialAd.show();
			}
			else
			{
				// Create ad request.
			    AdRequest adRequest = new AdRequest.Builder().build();

			    // Begin loading your interstitial.
			    interstitialAd.loadAd(adRequest);
			}
		}
		
			//Log.w("log:", "Caching Full Screen Ad");
		cb.cacheInterstitial();
		
    }


	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			toggle();
			return true;
		
		}
		return super.onOptionsItemSelected(item);
	}

	
	@Override
	public void didCacheInterstitial(String arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void didCacheMoreApps() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void didClickInterstitial(String arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void didClickMoreApps() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void didCloseInterstitial(String arg0) 
	{
		// TODO Auto-generated method stub
		if(this.cb.hasCachedInterstitial() == false)
		{
			this.cb.cacheInterstitial();
		}
	}

	@Override
	public void didCloseMoreApps() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void didDismissInterstitial(String arg0) 
	{
		// TODO Auto-generated method stub
		if(this.cb.hasCachedInterstitial() == false)
		{
			this.cb.cacheInterstitial();
		}
	}

	@Override
	public void didDismissMoreApps() {
		// TODO Auto-generated method stub
		
	}

	

	

	@Override
	public void didShowInterstitial(String arg0) 
	{
		// TODO Auto-generated method stub
		
	}

	@Override
	public void didShowMoreApps() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean shouldDisplayInterstitial(String arg0) 
	{
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean shouldDisplayLoadingViewForMoreApps() 
	{
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean shouldDisplayMoreApps() 
	{
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean shouldRequestInterstitial(String arg0) 
	{
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean shouldRequestInterstitialsInFirstSession() 
	{
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean shouldRequestMoreApps() 
	{
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void didFailToLoadInterstitial(String arg0, CBImpressionError arg1) 
	{
		// TODO Auto-generated method stub
		if(this.cb.hasCachedInterstitial() == false)
		{
			this.cb.cacheInterstitial();
		}
	}

	@Override
	public void didFailToLoadMoreApps(CBImpressionError arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void didFailToRecordClick(String arg0, CBClickError arg1) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean shouldPauseClickForConfirmation(CBAgeGateConfirmation arg0) {
		// TODO Auto-generated method stub
		return false;
	}
	

	
}
